<?php
namespace app\api\service;

use think\Controller;
use think\Cookie;
use think\Db;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/31
 * Time: 9:51
 */
class Index extends Controller{

    /**
     * 获取首页数据
     * @return array
     */
    public static function get_index_info($param){
        //启动事物
        try{
            $category = model('category')->field('id, name')->where(['delete' => 0, 'status' => 1])->select();
            $data['category'] = $category;
            return api_return('200',$data);
        } catch (\Exception $e) {
            // 回滚事务
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 根据分类和定位获取店铺列表
     * @param $param
     * @return array
     */
    public static function get_shop_list_by_category_and_lat_lng($param) {
        //启动事物
        try{
            $where['sc.category_id'] = $param['category_id'];
            // 根据用户经纬度 获取附近活动
            $coord = getAround($param['lat'], $param['lng'], 10000);
            $where['s.lat'] = ['between', [$coord[0], $coord[1]]];
            $where['s.lng'] = ['between', [$coord[2], $coord[3]]];
            //return api_return('200',$coord);
            $data = model('shop_category')->alias('sc')->field('s.logo, s.id, s.lat, s.lng, s.name, s.mobile, s.address')
                ->join('jx_shop s', 's.id = sc.shop_id')
                ->where($where)
                ->select();
            $return_data = [];
            foreach ($data as $k => &$v) {
                $v['logo'] = get_img_url($v->logo);
            }
            $return_data['list'] = $data;
            if (isset($param['member_id'])) {
                $count_num = Base::get_count_num($param);
                $return_data['count_num'] = $count_num['data'];
            }

            return api_return('200',$return_data);
        } catch (\Exception $e) {
            // 回滚事务
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 根据分类和定位获取店铺列表
     * @param $param
     * @return array
     */
    public static function get_shop_list_by_lat_lng($param) {
        //启动事物
        try{
            // 根据用户经纬度 获取附近活动
            $coord = getAround($param['lat'], $param['lng'], 10000);
            $where['lat'] = ['between', [$coord[0], $coord[1]]];
            $where['lng'] = ['between', [$coord[2], $coord[3]]];
            //return api_return('200',$coord);
            $shop = model('shop')->where($where)->whereOr('id', '=', 12)->select();

            $data = [];
            foreach ($shop as $k => $v) {
                $data[] = [
                    'id' => $v->id,
                    'logo' => get_img_url($v->logo),
                    'name' => $v->name,
                    'level' => $v->commentLevel() > 0 ? $v->commentLevel() : 5,
                    'order_num' => $v->orders()->where('status', 'gt', 1)->where('status', 'neq', 7)->count(),
                    'lat' => $v->lat,
                    'lng' => $v->lng
                ];
            }

            return api_return('200',$data);
        } catch (\Exception $e) {
            // 回滚事务
            return api_return(0, [], $e->getMessage());
        }
    }
}